home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 11041 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.5 KB

  1. Path: lrz-muenchen.de!news
  2. From: watzka@stat.uni-muenchen.de (Kurt Watzka)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Forward Struct Reference
  5. Date: 21 Mar 1996 16:39:48 GMT
  6. Organization: Leibniz-Rechenzentrum, Muenchen (Germany)
  7. Distribution: world
  8. Message-ID: <4is0ok$7ju@sparcserver.lrz-muenchen.de>
  9. References: <4iqu1p$edq@newsbf02.news.aol.com>
  10. NNTP-Posting-Host: sun2.lrz-muenchen.de
  11.  
  12. shinjinrui@aol.com (ShinJinRui) writes:
  13.  
  14. >What is the best way to declare a typdef for both a struct and a contained
  15. >function that operates on it?  I want to have a structure that contains a
  16. >pointer to a function that operates on the structure.
  17.  
  18. >example:
  19. >/* 
  20. >*       declare typedef to a function "FuncType" which operates on a
  21. >pointer
  22. >*       to a struct of type "structType"  (typedef of structType follows
  23. >below).
  24. >*/
  25. >typedef void FuncType(*structType);
  26.  
  27. Even if the definition of "structType" precedes this prototype, it
  28. is still wrong. You wanted to write
  29.  
  30.   typedef void FuncType(structType *);
  31.  
  32. BTW, in most cases defining a function type is _not_ what you want.
  33.  
  34. Since "structType" has not been introduced at this point, and you
  35. obviously want to avoid that, you can use an incomplete type. This
  36. can be done in at least two different ways.
  37.  
  38. 1.) You can use the struct "tag". A struct that has not been
  39.     introduced is an incomplete type.
  40.  
  41.     typedef void FuncType(struct structType_tag *);
  42.  
  43. 2.) You can use the incomplete type in a typedef statement.
  44.  
  45.     typedef struct structType_tag structType;
  46.  
  47.     typedef void FuncType(structType *);
  48.  
  49. >/* 
  50. >*        Declare the structure's type 
  51. >*/
  52. >typedef struct structType_tag { 
  53. >    int a;
  54. >    int b;
  55. >    void (*FuncType)(*structType);   
  56.  
  57. What is this supposed to do? If you want the struct to have a member
  58. that is a pointer to a function that takes a struct as an argument,
  59. there are some ways to do this:
  60.  
  61. 1.) Straightforward:
  62.  
  63.    typedef struct structType_tag {
  64.       int a;
  65.       int b;
  66.       void (* f)(struct structType_tag *);
  67.    } structType;
  68.  
  69. 2.) Using you typedef statements from above:
  70.  
  71.    typedef struct structType_tag structType;
  72.    typedef void FuncType(structType *);
  73.  
  74.    struct structType_tag
  75.    {
  76.       int a;
  77.       int b;
  78.       FuncType *f;
  79.    };
  80.  
  81. 3.) Using a function pointer type (This is a common approach)
  82.  
  83.    typedef void (* FuncPtr)(struct structType_tag *);
  84.    typedef struct structType_tag
  85.    {
  86.       int a;
  87.       int b;
  88.       FuncPtr f;
  89.    } structType;
  90.  
  91. Kurt
  92. --
  93. | Kurt Watzka                             Phone : +49-89-2180-6254
  94. | watzka@stat.uni-muenchen.de
  95. | ua302aa@sunmail.lrz-muenchen.de
  96.